package cn.com.member.controller.mine;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiAddress;
import cn.com.api.entity.ApiT_address;
import cn.com.api.entity.ApiTmember;
import cn.com.api.service.ApiAddressService;
import cn.com.api.service.ApiT_addressService;
import cn.com.util.JsonUtil;
import cn.com.util.ReturnObj;
import cn.com.util.Util;
import cn.com.util.Utils;

@Controller
public class AddressController {

	@Autowired
	private ApiAddressService addressService;
	@Autowired
	private ApiT_addressService apiT_addressService;

	private static Logger logger = Logger.getLogger(AddressController.class);

	/**
	 * @Title: getAddresslist
	 * @Description: 查询用户所有的地址
	 * @throws Exception
	 * @return void
	 */
	@RequestMapping("address-list")
	public ModelAndView getAddresslist(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> ret_map = new HashMap<String, Object>();
		ModelAndView mv = null;
		try {
			ApiTmember member = (ApiTmember) request.getSession().getAttribute("webuser");// 登陆用户
			List<ApiAddress> addressList = addressService.getAddressList(member.getCid().toString());
			ret_map.put("address_list", addressList);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));

		}
		mv = new ModelAndView("/member/address", ret_map);
		return mv;
	}

	/**
	 * @Title: address-insert-update
	 * @Description: 添加收货地址
	 * @throws Exception
	 * @return void
	 */
	@RequestMapping(value = "/address-insert-update")
	public ModelAndView address_insertOrUpdate(ApiT_address apiAddress, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> ret_map = new HashMap<String, Object>();
		ModelAndView mv = null;
		String addressid = request.getParameter("addressid");
		String u_id = request.getParameter("u_id");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String county = request.getParameter("county");
		String address = request.getParameter("address");
		String phone = request.getParameter("phone");
		String name = request.getParameter("name");
		String enable = request.getParameter("enable");

		try {
			ApiTmember member = (ApiTmember) request.getSession().getAttribute("webuser");// 登陆用户
			ApiT_address t_address = new ApiT_address();
			t_address.setU_id(member.getCid().toString());
			t_address.setProvince(province);
			t_address.setCity(city);
			t_address.setAddress(address);
			t_address.setPhone(phone);
			t_address.setName(name);
			if (Util.isNull(addressid)) {
				if (Util.isNull(apiT_addressService.getAddressesByUserId(member.getCid().toString()))) {
					t_address.setEnable(1);
				} else {
					t_address.setEnable(0);
				}
				apiT_addressService.insertT_address(t_address);
				ret_map.put(Utils.MESSAGE, "添加成功！");
			} else {
				if (!Util.isNull(enable)) {
					apiT_addressService.updateEnableById(addressid);
					if (enable.equals("1")) {
						apiT_addressService.updateEnableZero(member.getCid().toString(), addressid);
					}
				} else {
					t_address.setId(Integer.valueOf(addressid));
					apiT_addressService.updateT_address(t_address);
				}
				ret_map.put(Utils.MESSAGE, "修改成功！");
			}
			ret_map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			List<ApiAddress> addressList = addressService.getAddressList(member.getCuserid());
			ret_map.put("address_list", addressList);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
		}
		//
		mv = new ModelAndView("/member/address", ret_map);
		return mv;
	}

	// 获取地址信息
	/**
	 * @Title: getAddresslist
	 * @Description: 查询用户所有的地址
	 * @throws Exception
	 * @return void
	 */
	@RequestMapping("/addressInfo")
	public void update_address(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ReturnObj baseObj = new ReturnObj();
		try {
			String addressId = request.getParameter("addressId");
			ApiAddress address = addressService.getAddressById(addressId);
			JsonUtil.printJson(response, JsonUtil.object2json(address));
		} catch (Exception e) {
			e.printStackTrace();
			baseObj.handleException(e);
			JsonUtil.printJson(response, JsonUtil.object2json(baseObj));
		}
	}

	// 修改收货地址
	/**
	 * @Title: getAddresslist
	 * @Description: 查询用户所有的地址
	 * @throws Exception
	 * @return void
	 */
	@RequestMapping("/update_address")
	public ModelAndView update_address(@ModelAttribute ApiAddress addres, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> ret_map = new HashMap<String, Object>();
		ModelAndView mv = null;
		try {
			ApiTmember member = (ApiTmember) request.getSession().getAttribute("webuser");// 登陆用户
			Integer enable = addres.getEnable();
			if (enable == null && "".equals(enable)) {
				addres.setEnable(0);
			}
			if (enable == 1) {
				addressService.updateAllEnable(member.getCid().toString());
			}
			addressService.update(addres);

			List<ApiAddress> addressList = addressService.getAddressList(member.getCid().toString());
			ret_map.put("address_list", addressList);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
		}
		mv = new ModelAndView("/member/address", ret_map);
		return mv;
	}

	// 停用所有地址、启用选中地址
	/**
	 * @Title: getAddresslist
	 * @Description: 查询用户所有的地址
	 * @throws Exception
	 * @return void
	 */
	@RequestMapping("/updateSate_address")
	public ModelAndView updateSate_address(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> ret_map = new HashMap<String, Object>();
		ModelAndView mv = null;
		try {
			ApiTmember member = (ApiTmember) request.getSession().getAttribute("webuser");// 登陆用户
			String addressId = request.getParameter("addressId");
			addressService.updateAllEnable(member.getCid().toString());
			addressService.updateSignStart(Integer.valueOf(addressId));

			List<ApiAddress> addressList = addressService.getAddressList(member.getCid().toString());
			ret_map.put("address_list", addressList);
		} catch (Exception e) {
			e.printStackTrace();

		}
		mv = new ModelAndView("/member/address", ret_map);
		return mv;
	}

	// 删除地址
	/**
	 * @Title: getAddresslist
	 * @Description: 查询用户所有的地址
	 * @throws Exception
	 * @return void
	 */
	@RequestMapping("/del_address")
	public ModelAndView delteaddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> ret_map = new HashMap<String, Object>();
		ModelAndView mv = null;
		try {
			String addressId = request.getParameter("addressId");
			addressService.deleteAddr(Integer.valueOf(addressId));
			ApiTmember member = (ApiTmember) request.getSession().getAttribute("webuser");// 登陆用户
			List<ApiAddress> addressList = addressService.getAddressList(member.getCid().toString());
			ret_map.put("address_list", addressList);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
		}
		mv = new ModelAndView("/member/address", ret_map);
		return mv;
	}

}
